package com.meiyu.live.tv.widget;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.popj.GiftForLive;
import com.meiyu.live.ui.mine.RechargeActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zbl on 2016/9/11.
 */
public class GiftSendView extends FrameLayout implements View.OnClickListener {

    public static final int COUNT_PER_PAGE = 8;

    private Context context;
    private View btn_send, btn_recharge;
    private TextView tv_moneyleft;

    private ViewPager viewPager;
    private GiftPagerAdapter pagerAdapter;
    private List<GiftForLive> giftList = new ArrayList<>();

    private GiftSendViewListener listener;

    @Override
    public void onClick(View view) {
        if (view == btn_send) {
            if (listener != null) {
                for (GiftForLive bean : giftList) {
                    if (bean.isSelected) {
                        listener.onSendGift(bean);
                        break;
                    }
                }
            }
        } else if (view == btn_recharge) {
            Intent intent = new Intent(context, RechargeActivity.class);
            context.startActivity(intent);
        }
    }

    public interface GiftSendViewListener {
        void onSendGift(GiftForLive giftForLive);
    }

    public GiftSendView(Context context) {
        super(context);
        init(context);
    }

    public GiftSendView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.view_giftgrid, this);
        btn_send = findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);
        btn_recharge = findViewById(R.id.btn_recharge);
        btn_recharge.setOnClickListener(this);
        tv_moneyleft = (TextView) findViewById(R.id.tv_moneyleft);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        pagerAdapter = new GiftPagerAdapter(((BaseActivity) context).getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
    }

    public void setGiftSendViewListener(GiftSendViewListener listener) {
        this.listener = listener;
    }

    public void setData(List<GiftForLive> list) {
        if (list != null && list.size() > 0) {
            giftList.clear();
            giftList.addAll(list);
            if (pagerAdapter != null) {
                pagerAdapter.notifyDataSetChanged();
            }
        }
    }

    public void setCoins(long coins) {
        tv_moneyleft.setText("剩余钻石数：" + coins);
    }

    class GiftPagerAdapter extends FragmentPagerAdapter {

        public GiftPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            GiftPageFragment fragment = GiftPageFragment.newInstance(position);
            fragment.setData(giftList);
            return fragment;
        }

        @Override
        public int getCount() {
            return (int) Math.ceil((float) giftList.size() / COUNT_PER_PAGE);
        }
    }

}
